<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-attentionforbid"></i> 权限管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>接口管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :span="6">
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            class="handle-del mr10"
                            @click="delAllSelection"
                        >批量删除</el-button>
                        <el-button
                            type="primary"
                            icon="el-icon-circle-plus-outline"
                            class="handle-del mr10"
                            @click="addApi"
                        >添加API</el-button>
                    </el-col>
                    <el-col :span="12">
                        <el-select v-model="query.sort" placeholder="选择排序方式">
                            <el-option key="1" label="正序" value="1"></el-option>
                            <el-option key="2" label="倒序" value="2"></el-option>
                        </el-select>
                        <el-input v-model="query.name" placeholder="API" class="handle-input mr10"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    </el-col>
                </el-row>
            </div>
            <el-table
                :data="tableData"
                border
                v-loading="loading"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="名称"></el-table-column>
                <el-table-column prop="api" label="API"></el-table-column>
                <el-table-column prop="method" label="请求方式"></el-table-column>
                <el-table-column prop="explain" label="说明"></el-table-column>
                <el-table-column prop="sort" label="排序"></el-table-column>
                <el-table-column prop="updated_at" label="修改时间"></el-table-column>
                <el-table-column prop="created_at" label="添加时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog v-dialogDrag :title="formName" :visible.sync="editVisible" width="30%">
            <el-form ref="editData" :model="editData" label-width="70px">
                <el-form-item label="名称">
                    <el-input
                        v-model="editData.name"
                        maxlength="30"
                        minlength="2"
                        placeholder="请输入API名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="API">
                    <el-input
                        v-model="editData.api"
                        maxlength="30"
                        minlength="2"
                        placeholder="请输入API地址"
                    ></el-input>
                </el-form-item>
                <el-form-item label="请求">
                    <el-select v-model="editData.method" placeholder="请选择请求方式">
                        <el-option key="1" label="GET" value="GET"></el-option>
                        <el-option key="2" label="POST" value="POST"></el-option>
                        <el-option key="3" label="PUT" value="PUT"></el-option>
                        <el-option key="4" label="DELETE" value="DELETE"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="说明">
                    <el-input v-model="editData.explain" maxlength="50" placeholder="请输入API说明"></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input
                        v-model="editData.sort"
                        maxlength="2"
                        type="number"
                        placeholder="从小到大排序"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 添加弹出 -->
        <el-dialog v-dialogDrag :title="formName" :visible.sync="addVisible" width="30%">
            <el-form ref="addData" :model="addData" label-width="70px">
                <el-form-item label="名称">
                    <el-input
                        v-model="addData.name"
                        maxlength="30"
                        minlength="2"
                        placeholder="请输入API名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="API">
                    <el-input
                        v-model="addData.api"
                        maxlength="30"
                        minlength="2"
                        placeholder="请输入API地址"
                    ></el-input>
                </el-form-item>
                <el-form-item label="请求">
                    <el-select v-model="addData.method" placeholder="请选择请求方式">
                        <el-option key="1" label="GET" value="GET"></el-option>
                        <el-option key="2" label="POST" value="POST"></el-option>
                        <el-option key="3" label="PUT" value="PUT"></el-option>
                        <el-option key="4" label="DELETE" value="DELETE"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="说明">
                    <el-input v-model="addData.explain" maxlength="50" placeholder="请输入API说明"></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input
                        v-model="addData.sort"
                        maxlength="2"
                        type="number"
                        placeholder="从小到大排序"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
  name: 'basetable',
  data () {
    return {
      query: {
        name: '',
        page: 1,
        pageSize: 10,
        sort: ''
      },
      addData: {
        api: '',
        name: '',
        method: '',
        explain: '',
        sort: 10
      },
      editData: {
        api: '',
        name: '',
        method: '',
        explain: '',
        sort: 10
      },
      formName: '添加',
      tableData: [],
      multipleSelection: [],
      pageTotal: 0,
      delList: [],
      loading: false,
      editVisible: false,
      addVisible: false,
      form: {},
      idx: -1,
      id: -1,
      //获取列表请求
      getList: function () {
        let that = this;
        this.api.get('getApi', this.query, res => {
          that.tableData = res.data.list;
          that.query.page = res.data.page;
          that.query.pageSize = res.data.pageSize;
          that.pageTotal = res.data.pageTotal;
        }, undefined, undefined, this);
      },
      //删除请求
      delLists: function (ids, msg) {
        let that = this;
        if (ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delApi(ids, function (response) {
            that.query.page = 1;
            that.getList();
            that.multipleSelection = [];
            that.$message.success(msg);
          });
        }).catch(() => { });
      }
    };
  },
  created () {
    let that = this;
    this.getList();

  },
  methods: {
    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },

    /***
     * 添加操作
     */
    addApi () {
      this.addVisible = true;
      this.formName = '添加';
    },

    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },

    // 删除操作
    handleDelete (index, row) {
      this.delLists([row.id], '删除成功');
    },
    //批量删除
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delLists(ids, `删除了${str}`);
    },
    // 编辑操作
    handleEdit (index, row) {
      this.formName = '编辑';
      this.idx = index;
      this.editData = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit () {
      let that = this;
      this.api.editApi(this.editData, function (response) {
        that.editVisible = false;
        that.$message.success(response.msg);
        that.getList();
      });
    },
    //保存添加
    saveAdd () {
      let that = this;
      this.api.addApi(this.addData, function (response) {
        that.addVisible = false;
        that.addData = {};
        that.tableData.push(that.addData);
        that.$message.success(response.msg);
        that.getList();
      });
    },
    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    }
  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
